/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager.visualizar;

import br.com.deployment.entidade.empresa.Cartorio;
import br.com.deployment.entidade.registroimovel.GeoRef;
import br.com.deployment.entidade.registroimovel.Imovel;
import br.com.deployment.servico.CartorioServico;
import br.com.deployment.servico.ImovelServico;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.map.OverlaySelectEvent;
import org.primefaces.model.map.*;

/**
 *
 * @author luciano
 */
@ManagedBean
@SessionScoped
public class ManagerVisualizarCartorio implements Serializable{

    @EJB
    CartorioServico cartorioServico;
    @EJB
    ImovelServico imovelServico;
    private Cartorio cartorioVisualizar;
    private Cartorio cartorio;
    private List<Cartorio> cartorios;
    private MapModel linaModel;
    private String centerMap;

    private String imovel;
    Marker marker;
    
    @PostConstruct
    public void init() {
        cartorio = new Cartorio();
    }

    public void buscar() {
        cartorios = cartorioServico.findCartorio(cartorio);
    }

    public List<Cartorio> todos() {
        return cartorioServico.cartorios();
    }

    public List<GeoRef> ordenarList(List<GeoRef> texto) {
        List<GeoRef> teste = new ArrayList<GeoRef>();
        teste = texto;
        Collections.sort(teste, new Comparator<GeoRef>() {

            @Override
            public int compare(GeoRef o1, GeoRef o2) {
                Integer x = o1.getId();
                Integer y = o2.getId();
                return x.compareTo(y);
            }
        });
        return teste;
    }

    public void constroiImoveis() {
        for (Imovel im : imovelServico.findAll()) {
            Polygon polygonInte = new Polygon();
            Double x = 0D, y = 0D;
            
            if (im.getObjeto() != null && im.getObjeto().getGeoReferenciamento() != null
                    && im.getObjeto().getGeoReferenciamento().size() > 1) {
                List<GeoRef> lst = im.getObjeto().getGeoReferenciamento();
                for (GeoRef geoRef : lst ) {
                    
                    x += geoRef.getLat() / lst.size();
                    y += geoRef.getLng() / lst.size();
                    
                    polygonInte.getPaths().add(new LatLng(geoRef.getLat(), geoRef.getLng()));
                    imovel = "<b>Registro: </b>"+im.getRegistro()+" <br/>"
                            + "<b>Tipo: </b>"+im.getObjeto().getTipo()+" <br/>"
                            + "<b>Livro: </b>"+im.getLivro().getTitulo()+"<b> Fls. </b>"+im.getFolhasString();
                    polygonInte.setData(imovel);
                    polygonInte.setStrokeColor("#FF9900");
                    polygonInte.setFillColor("#FF9900");
                    polygonInte.setStrokeOpacity(0.8);
                    polygonInte.setStrokeWeight(2);
                    polygonInte.setFillOpacity(0.7);
                    
                }
                linaModel.addOverlay(new Marker(new LatLng(x, y), imovel));
                linaModel.addOverlay(polygonInte);
            }
        }
    }

    public void construirLimitesCartorio() {
        this.centerMap = "-2.9136, -41.7577";
        linaModel = new DefaultMapModel();
        Polyline polyline = new Polyline();

        Double centerX = 0D;
        Double centerY = 0D;
        List<GeoRef> lst = ordenarList(cartorioVisualizar.getGeoAtuacao());
        for (GeoRef geoRef : lst) {
            polyline.getPaths().add(new LatLng(geoRef.getLat(), geoRef.getLng()));
            centerX += geoRef.getLat() / lst.size();
            centerY += geoRef.getLng() / lst.size();
        }
        centerMap = "" + centerX + ", " + centerY;
        polyline.setStrokeWeight(7);
        polyline.setStrokeColor("#C8343B");
        polyline.setStrokeOpacity(0.7);
        linaModel.addOverlay(polyline);
        constroiImoveis();
    }
     public void onMarkerSelect(OverlaySelectEvent event) {  
        Marker polygon = (Marker) event.getOverlay();
        imovel = polygon.getTitle();
    } 

    public void redirect(String string, Cartorio cartorio) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            cartorioVisualizar = cartorio;
            construirLimitesCartorio();
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerVisualizarCartorio.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Cartorio getCartorio() {
        return cartorio;
    }

    public MapModel getLinaModel() {
        return linaModel;
    }

    public String getImovel() {
        return imovel;
    }

    public void setImovel(String imovel) {
        this.imovel = imovel;
    }
    

    public void setLinaModel(MapModel linaModel) {
        this.linaModel = linaModel;
    }

    public void setCartorio(Cartorio cartorio) {
        this.cartorio = cartorio;
    }

    public Cartorio getCartorioVisualizar() {
        return cartorioVisualizar;
    }

    public void setCartorioVisualizar(Cartorio cartorioVisualizar) {
        this.cartorioVisualizar = cartorioVisualizar;
    }

    public List<Cartorio> getCartorios() {
        return cartorios;
    }

    public void setCartorios(List<Cartorio> cartorios) {
        this.cartorios = cartorios;
    }

    public String getCenterMap() {
        return centerMap;
    }

    public void setCenterMap(String centerMap) {
        this.centerMap = centerMap;
    }
}
